package com.example.entity;

import jdk.nashorn.internal.objects.annotations.Getter;
import jdk.nashorn.internal.objects.annotations.Setter;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @Author: wyp
 * @Date: 2021/9/8 17:50
 */
public class AtomicReferenceBean {
    public static void main(String[] args) {
        User a = new User("a",18);
        User b = new User("b",36);

        AtomicReference<User> atomicReference = new AtomicReference<>();
        atomicReference.set(a);

        System.out.println(atomicReference.compareAndSet(a,b)+"\t "+atomicReference.get().toString());
        System.out.println(atomicReference.compareAndSet(a,b)+"\t "+atomicReference.get().toString());

    }
}
class User{
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "AtomicReferenceBean{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
